package cn.wolfcode.service.impl;

import cn.wolfcode.common.web.Result;
import cn.wolfcode.domain.Product;
import cn.wolfcode.domain.SeckillProduct;
import cn.wolfcode.domain.SeckillProductVo;
import cn.wolfcode.feign.ProductFeignApi;
import cn.wolfcode.mapper.SeckillProductMapper;
import cn.wolfcode.mq.OrderMQResult;
import cn.wolfcode.redis.SeckillRedisKey;
import cn.wolfcode.service.ISeckillProductService;
import com.alibaba.fastjson.JSON;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.spring.core.RocketMQTemplate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Slf4j
public class SeckillProductServiceImpl implements ISeckillProductService {
    @Autowired
    private SeckillProductMapper seckillProductMapper;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private RocketMQTemplate rocketMQTemplate;
    @Autowired
    private ProductFeignApi productFeignApi;

    @Override
    public List<SeckillProductVo> selectByType(int type) {
        // 查询秒杀商品表数据 List<SeckillProduct>
        List<SeckillProduct> seckillProducts = seckillProductMapper.queryCurrentlySeckillProduct(type);
        // 循环遍历把ProductId 放到List<Long> ids
        List<Long> ids = new ArrayList<>();
        for (SeckillProduct seckillProduct : seckillProducts) {
            ids.add(seckillProduct.getProductId());
        }
        // 远程调用商品服务 查询商品数据 Result<List<Product>>
        Result<List<Product>> result = productFeignApi.queryByIds(ids);
        if (result != null && !result.hasError()) {
            List<Product> products = result.getData();
            List<SeckillProductVo> vos = new ArrayList<>();
            // 进行聚合操作，返回一个List<>
            Map<Long,Product> map = new HashMap<>();
            for (Product product : products) {
                map.put(product.getId(),product);
            }
            for (SeckillProduct seckillProduct : seckillProducts) {
                SeckillProductVo vo = new SeckillProductVo();
                Product product = map.get(seckillProduct.getProductId());
                BeanUtils.copyProperties(product,vo);
                BeanUtils.copyProperties(seckillProduct,vo);
                vos.add(vo);
            }
            return vos;
        }
        return null;
    }

    @Override
    public List<SeckillProductVo> queryByTime(int time) {
        // SeckillProductList:time
        List<Object> values = redisTemplate.opsForHash().values(SeckillRedisKey.SECKILL_PRODUCT_LIST.getRealKey(time + ""));
        List<SeckillProductVo> vos = new ArrayList<>();
        for (Object objJson : values) {
            vos.add(JSON.parseObject(objJson+"",SeckillProductVo.class));
        }
        return vos;
    }

    @Override
    public SeckillProductVo find(int time, Long seckillId) {
        Object objJson = redisTemplate.opsForHash().get(SeckillRedisKey.SECKILL_PRODUCT_LIST.getRealKey(time + ""), seckillId + "");
        if (StringUtils.isEmpty(objJson + "")) {
            return null;
        }
        return JSON.parseObject(objJson+"",SeckillProductVo.class);
    }

    @Override
    public void anaplerosis(OrderMQResult orderMQResult) {
        int stockCount = seckillProductMapper.getStockCount(orderMQResult.getSeckillId());
        redisTemplate.opsForHash().put(SeckillRedisKey.SECKILL_STOCK_COUNT_HASH.getRealKey(orderMQResult.getTime()+""),orderMQResult.getSeckillId()+"",stockCount+"");
        log.info("数据回补成功");
    }
}